package com.tebiecloud.mpweixin.server.controller.feign;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.tebiecloud.common.model.ResultBody;
import com.tebiecloud.common.utils.ResultBodyUtils;
import com.tebiecloud.mpweixin.client.model.entity.MpminiAccountRef;
import com.tebiecloud.mpweixin.client.service.IMpminiAccountRefServiceClient;
import com.tebiecloud.mpweixin.server.service.MpminiAccountRefService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * @Author: tebie
 * @Date: 2019-09-24 19:56
 */
@Slf4j
@RestController
@RequestMapping("/feign/mpminiaccountref")
public class AccountRefController implements IMpminiAccountRefServiceClient {

    @Autowired
    private MpminiAccountRefService mpminiAccountRefService;

    /**
     * 获取最新的关联信息
     * @param accountId
     * @return
     */
    @PostMapping("infoMpminiAccountRef")
    @Override
    public ResultBody<MpminiAccountRef> getLastMpminiAccountRef(@RequestParam("accountId") long accountId) {
        MpminiAccountRef mpminiAccountRef = mpminiAccountRefService.getLastMpminiAccountRef(accountId);
        if(mpminiAccountRef != null) {
            return ResultBodyUtils.ok(mpminiAccountRef);
        }

        return ResultBodyUtils.failed();
    }

    /**
     * 添加关联信息
     * @param accountId
     * @param accountUsernameRef
     * @return
     */
    @PostMapping("addMpminiAccountRef")
    @Override
    public ResultBody addMpminiAccountRef(@RequestParam("accountId") long accountId,
                                          @RequestParam("accountUsernameRef") String accountUsernameRef) {
        MpminiAccountRef mpminiAccountRef = new MpminiAccountRef();
        mpminiAccountRef.setAccountId(accountId);
        mpminiAccountRef.setAccountUsernameRef(accountUsernameRef);
        mpminiAccountRefService.save(mpminiAccountRef);
        return ResultBodyUtils.ok();
    }

    @PostMapping("removeMpminiAccountRef")
    @Override
    public ResultBody removeMpminiAccountRef(@RequestParam("accountId") long accountId, @RequestParam("account") String account) {
        QueryWrapper<MpminiAccountRef> mpminiAccountRefQueryWrapper = new QueryWrapper<>();
        if(accountId > 0) {
            mpminiAccountRefQueryWrapper.eq("account_id", accountId);
        }
        mpminiAccountRefQueryWrapper.eq("account_username_ref",account);
        mpminiAccountRefService.remove(mpminiAccountRefQueryWrapper);
        return ResultBodyUtils.ok();
    }
}
